package net.lijingye.education.controller;

import com.alibaba.fastjson.JSONException;
import net.lijingye.education.response.BaseResponse;
import net.lijingye.education.response.ErrorResponse;
import net.lijingye.education.response.meta.InternalErrorMeta;
import net.lijingye.education.response.meta.JSONParameterErrorMeta;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by LijingYe on 2016/3/26.
 */
public abstract class BaseController {
    @Value("${util.page_size}")
    private int pageSize;

    public int getPageSize() {
        return pageSize;
    }

    @ExceptionHandler
    public BaseResponse handleException(HttpServletRequest request, Exception e) {
        e.printStackTrace();
        if (e instanceof JSONException) {
            return new ErrorResponse(new JSONParameterErrorMeta());
        }
        return new ErrorResponse(new InternalErrorMeta());
    }
}
